﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kokomo.Lexical.Coding;
using System.Reflection.Emit;
using System.Reflection;
using Kokomo.Lexical.Expressions;

namespace Kokomo.PeInspector.Reflection.Disassembly.Flow
{
	class MethodFlowAnalyzer : FlowAnalyzer
	{
		public MethodFlowAnalyzer(MsilDecompilationContext context, int length)
			: base(context, 0, length, 0, length)
		{
			this.Statements = new List<Statement>();
			this.ActiveStatementBlock = this.Statements;
		}

		public List<Statement> Statements { get; private set; }

		protected override void AnalyzeInstruction()
		{
			Instruction instruction = this.Context.CurrentInstruction;

			if (
				(instruction.NextOffset == this.BlockEndOffset)
				&& (instruction.OpCode == OpCodes.Ret)
				&& (this.Context.ExpressionCount == 0)
				)
			{
				// Omit final return statement
				this.Context.Advance();
			}
			else
			{
				base.AnalyzeInstruction();
			}
		}
	}
}
